let express = require('express');
let path = require('path');
let app = express();
let config = require('./js/config.js');

const cors = require('cors');
app.use(cors()); //允许所有来源的请求

// 解析application/json类型的数据
app.use(express.json());
// 解析application/x-www-form-urlencoded类型的数据
app.use(express.urlencoded({extended: true}));

// 挂载一个响应中间件
let {responseMiddleware} = require('./js/middleware/responseMiddleware.js');
app.use(responseMiddleware);

// 静态资源中间件
app.use(express.static(path.join(__dirname, 'public')));

// 路由设置
let routes = require('./routes/routes');
app.use('/', routes);
app.all('*', (req, res, next) => {
    res.error(config.result.notFound)
});

app.listen(config.port, () => {
    console.log('express server running at http://127.0.0.1:' + config.port)
})